package com.yc.tomcat2.javax.servlet.http;

import com.yc.tomcat2.javax.servlet.YcServlet;
import com.yc.tomcat2.javax.servlet.YcServletRequset;
import com.yc.tomcat2.javax.servlet.YcServletResponse;

/**
 * @author zwei
 * @create 2024-07-27 21:18
 */
public abstract class YcHttpServlet implements YcServlet {
    @Override
    public void init() {

    }

    public void doPost(YcHttpServletRequest req, YcHttpServletResponse resp) {

    }

    public void doGet(YcHttpServletRequest req, YcHttpServletResponse resp) {

    }

    public void doHead(YcHttpServletRequest req, YcHttpServletResponse resp) {

    }

    public void doDelete(YcHttpServletRequest req, YcHttpServletResponse resp) {

    }

    public void doTrace(YcHttpServletRequest req, YcHttpServletResponse resp) {

    }

    public void doOption(YcHttpServletRequest req, YcHttpServletResponse resp) {

    }

    @Override
    public void destroy() {

    }

    @Override   //在service中判断 method是什么 再调用对应的doXXX方法
    public void service(YcServletRequset requset, YcServletResponse response) {
        //从request中取出method(http协议特有)
        String method = ((YcServletRequset) requset).getMethod();
        if ("get".equalsIgnoreCase(method)) {
            doGet((YcHttpServletRequest) requset, (YcHttpServletResponse) response);
        } else if ("post".equalsIgnoreCase(method)) {
            doPost((YcHttpServletRequest) requset, (YcHttpServletResponse) response);
        } else if ("head".equalsIgnoreCase(method)) {
            doHead((YcHttpServletRequest) requset, (YcHttpServletResponse) response);
        } else if ("delete".equalsIgnoreCase(method)) {
            doDelete((YcHttpServletRequest) requset, (YcHttpServletResponse) response);
        } else if ("trace".equalsIgnoreCase(method)) {
            doTrace((YcHttpServletRequest) requset, (YcHttpServletResponse) response);
        } else if ("option".equalsIgnoreCase(method)) {
            doOption((YcHttpServletRequest) requset, (YcHttpServletResponse) response);
        }else {
            //TODO  错误的响应
        }
    }
    public void service(YcHttpServletRequest requset, YcHttpServletResponse response) {
        service(requset,response);
    }
}
